package com.lzh.redisdemo;

import com.lzh.redisdemo.pojo.Grade;
import com.lzh.redisdemo.pojo.Role;
import com.lzh.redisdemo.pojo.Student;
import com.lzh.redisdemo.pojo.User;
import com.lzh.redisdemo.repository.RoleRepository;
import com.lzh.redisdemo.repository.StudentJpaRepository;
import com.lzh.redisdemo.repository.UserMethodRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
 * @Classname StudentAndGrade
 * @Description 多表关联操作
 * @Date 2021/11/4 11:21
 * @Created by liuzihao
 */
@SpringBootTest
public class StudentManyToManyAndSoOn {

    @Autowired
    StudentJpaRepository studentJpaRepository;

    @Autowired
    UserMethodRepository userMethodRepository;

    @Autowired
    RoleRepository roleRepository;
    @Test
    void oneToOne(){
        Grade grade = new Grade("三年级");
        Student student = new Student();
        student.setName("卡尔3");
        student.setGrade(grade);
        studentJpaRepository.save(student);
    }

    @Test
    void manyToOne(){
        //角色
        Role role = new Role();
        role.setRoleName("系统管理员");
        //多个用户
        User user = new User();
        user.setUserName("多对一3");
        user.setRole(role);
        User user2 = new User();
        user2.setUserName("多对一4");
        user2.setRole(role);

        role.getUsers().add(user);
        role.getUsers().add(user2);

        //保存
        roleRepository.save(role);
    }
}
